package com.bhs.sansonglogistics.dialog;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.content.Context;
import android.graphics.Color;
import android.view.View;
import android.widget.DatePicker;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bhs.sansonglogistics.R;
import com.bhs.sansonglogistics.bean.LineListBean;
import com.bhs.sansonglogistics.bean.event.Event;
import com.bhs.sansonglogistics.bean.event.ScreeningEvent;
import com.bhs.sansonglogistics.ui.waybill.UpstreamShipmentActivity;
import com.bhs.sansonglogistics.utils.DateUtils;
import com.bhs.sansonglogistics.view.AutoLineFeedLayoutManager;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.loper7.date_time_picker.DateTimeConfig;
import com.loper7.date_time_picker.dialog.CardDatePickerDialog;
import com.lxj.xpopup.impl.PartShadowPopupView;

import org.greenrobot.eventbus.EventBus;

import java.util.Calendar;
import java.util.List;

public class TopScreenDialog implements View.OnClickListener {

    private LinearLayout mLlTime;
    private TextView mTvTime;
    private RecyclerView mRvList;
    private TextView mTvReset;
    private TextView mTvConfirm;
    private BaseQuickAdapter<LineListBean, BaseViewHolder> mAdapter;

    private UpstreamShipmentActivity activity;

    public TopScreenDialog(@NonNull UpstreamShipmentActivity context) {
        this.activity = context;
        initView();
        mAdapter = new BaseQuickAdapter<LineListBean, BaseViewHolder>(R.layout.item_line) {
            @Override
            protected void convert(@NonNull BaseViewHolder helper, LineListBean item) {
                TextView tvName = helper.getView(R.id.tv_name);
                tvName.setText(item.getLine_name());
                tvName.setBackgroundResource(item.isChecked() ? R.drawable.bg_blue_14 : R.drawable.border_black_14);
                tvName.setTextColor(item.isChecked() ? mContext.getColor(R.color.white) : mContext.getColor(R.color.black));

            }
        };
        mRvList.setAdapter(mAdapter);
        mRvList.setLayoutManager(new AutoLineFeedLayoutManager());
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @SuppressLint("NotifyDataSetChanged")
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                for (LineListBean datum : mAdapter.getData()) {
                    datum.setChecked(false);
                }
                mAdapter.getData().get(position).setChecked(true);
                mAdapter.notifyDataSetChanged();
            }
        });
    }


    private void initView() {
        mLlTime = activity.findViewById(R.id.ll_time);
        mTvTime = activity.findViewById(R.id.tv_time);
        mRvList = activity.findViewById(R.id.rv_list);
        mTvReset = activity.findViewById(R.id.tv_reset);
        mTvConfirm = activity.findViewById(R.id.tv_confirm);
        mLlTime.setOnClickListener(this);
        mTvReset.setOnClickListener(this);
        mTvConfirm.setOnClickListener(this);
    }

    public void setData(List<LineListBean> data) {
        mAdapter.setNewData(data);
    }

    @SuppressLint("NotifyDataSetChanged")
    @Override
    public void onClick(View view) {
        if (view.getId() == R.id.ll_time) {
            new CardDatePickerDialog.Builder(activity)
                    .setTitle("选择时间")
                    .showBackNow(false)
                    .setDisplayType(DateTimeConfig.YEAR, DateTimeConfig.MONTH, DateTimeConfig.DAY)
                    .setThemeColor(Color.parseColor("#485EF4"))
                    .setOnChoose("确定", aLong -> {
                        mTvTime.setText(DateUtils.getYearMonthDay(aLong));
                        return null;
                    }).build().show();
        } else if (view.getId() == R.id.tv_reset) {
            mTvTime.setText("");
            for (LineListBean datum : mAdapter.getData()) {
                datum.setChecked(false);
            }
            mAdapter.notifyDataSetChanged();
        } else if (view.getId() == R.id.tv_confirm) {
            String lineId = null;
            for (LineListBean datum : mAdapter.getData()) {
                if (datum.isChecked()) {
                    lineId = String.valueOf(datum.getLine_id());
                }
            }
            activity.mFlScreen.setVisibility(View.GONE);
            EventBus.getDefault().post(new ScreeningEvent(lineId, mTvTime.getText().toString(), mTvTime.getText().toString()));
        }
    }

}
